package com.fastCms.business.frame.busi.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastCms.business.bim.daos.CategoryMapper;
import com.fastCms.business.bim.enums.CategroySceneEnum;
import com.fastCms.business.bim.pojo.po.CategorySavePO;
import com.fastCms.business.bim.pojo.po.ChangeSortPO;
import com.fastCms.business.bim.pojo.po.ChangeStatusPO;
import com.fastCms.business.bim.pojo.po.ScenePO;
import com.fastCms.business.bim.pojo.vo.CategoryVO;
import com.fastCms.business.bim.service.CategoryService;
import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.frame.daos.NewsMapper;
import com.fastCms.business.frame.exception.BusinessException;
import com.fastCms.business.frame.pojo.entity.Category;
import com.fastCms.business.frame.pojo.po.IdStrPO;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import com.fastCms.business.frame.utils.BeanConvertUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 行业管理 服务实现类
 * </p>
 *
 * @author quan
 * @since 2024-05-23
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    private NewsMapper newsMapper;

    @Override
    public List<CategoryVO> showList(ScenePO param) {
        String scene = param.getScene();
        LambdaQueryWrapper<Category> bannerQueryWrapper = new LambdaQueryWrapper();
        bannerQueryWrapper.eq(Category::getScene, scene);
        bannerQueryWrapper.eq(Category::getOrganizeCode, param.getOrganizeCode());
        bannerQueryWrapper.eq(Category::getState, CommonConst.STATE.VALID);
        List<Category> list = list(bannerQueryWrapper);
        if (CollUtil.isEmpty(list)) {
            List<CategoryVO> bannerVOList = BeanConvertUtil.convertList(list, CategoryVO.class);
            return bannerVOList;
        }
        return Lists.newArrayList();
    }

    @Override
    public Boolean saveOrUpdate(CategorySavePO param) throws BusinessException {
        if (StringUtils.isBlank(param.getId())) {
            Category category = new Category();
            BeanUtil.copyProperties(category, param);
            this.save(category);
        } else {
            Category category = getById(param.getId());
            if (category == null) {
                throw new BusinessException("数据不存在");
            }
            BeanUtil.copyProperties(category, param);
            this.updateById(category);
        }
        return true;
    }

    @Override
    public ApiResponse<Boolean> delete(IdStrPO param) throws BusinessException {
        Category category = getById(param.getId());
        if (category == null) {
            throw new BusinessException("数据不存在");
        }
        category.setState(CommonConst.STATE.INVALID);
        //寻找具体的业务是否有关联数据
        String scene = category.getScene();
        if (scene.equals(CategroySceneEnum.CATEGORY_NEWS.getCode())) {
            int count = newsMapper.countValidByCategoryId(param.getId());
            if (count > 0) {
                return ResponseUtil.fail("关联可用的数据,请先检查!");
            }
            category.setState(CommonConst.STATE.INVALID);
            this.updateById(category);
        }

        return ResponseUtil.success(true);
    }

    @Override
    public Boolean changeStatus(ChangeStatusPO param) throws BusinessException {
        Category category = getById(param.getId());
        if (category == null) {
            throw new BusinessException("数据不存在");
        }
        category.setStatus(param.getStatus());
        this.updateById(category);
        return true;
    }

    @Override
    public Boolean changeSort(List<ChangeSortPO> param) {
        param.stream().forEach(index -> {
            Category category = getById(index.getId());
            if (category != null) {
                category.setId(index.getId());
                category.setSort(index.getSort());
                updateById(category);
            }
        });
        return true;
    }

}
